package com.bytenebula.asteroid.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.bytenebula.ByteNebulaException;
import com.bytenebula.Consts;
import com.bytenebula.asteroid.ResponseResult;
import com.bytenebula.ResultCodes;
import com.bytenebula.entity.BnAccount;
import com.bytenebula.asteroid.entity.LoginResult;
import com.bytenebula.asteroid.entity.LoginParams;
import com.bytenebula.asteroid.service.BnAccountService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Objects;

@Tag(name = "Authority User Login Controller")
@RestController
@RequestMapping("/auth")
@ResponseResult
public class LoginController {

    @Autowired
    private BnAccountService accountService;

    @PostMapping("login")
    public LoginResult login(@RequestBody LoginParams params) {
        if (params == null)
            throw new ByteNebulaException(ResultCodes.INVALID_PARAMETER, "params");
        if (params.getUserName() == null)
            throw new ByteNebulaException(ResultCodes.INVALID_PARAMETER, "userName");
        if (params.getPassword() == null)
            throw new ByteNebulaException(ResultCodes.INVALID_PARAMETER, "password");

        LoginResult result = new LoginResult();

        // 验证用户名和密码
        BnAccount account = accountService.getOneByUserName(params.getUserName());
        if (account == null)
            result.setStatus(ResultCodes.CANNOT_FIND_ACCOUNT.getCode());
        assert account != null;
        if (!Objects.equals(account.getPassword(), params.getPassword()))
            result.setStatus(ResultCodes.ACCOUNT_LOGIN_FAILED.getCode());

        // 登录
        StpUtil.login(account.getUserName());
        SaSession session = StpUtil.getTokenSession();
        session.set(Consts.SESS_CURRENT_USER, account.getUser());
        session.set(Consts.SESS_TENANT_ID, account.getUser().getTenantId());
        result.setStatus(ResultCodes.SUCCESS.getCode());
        result.setUser(account.getUser());
        result.setLoginTime(LocalDateTime.now());

        String token = StpUtil.getTokenValue();
        result.setToken(token);

        return result;
    }

    @PostMapping("logout")
    public LoginResult logout(@RequestBody LoginParams params) {
        StpUtil.logout(params.getUserName());
        return new LoginResult(ResultCodes.SUCCESS.getCode());
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @PostMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @PostMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenSession());
    }
}
